package com.broadcaster.broadcaster.core.tool;

import com.broadcaster.broadcaster.core.Broadcaster;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PostProcessors implements BeanPostProcessor {
    /**
    * 异步系统回调的消费者可能会导致异常，所以需要记录广播者和被消费的路径的记录
    *
    * */
    public static Map<String,String> mappingMap=new ConcurrentHashMap<>();

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        Method method[]= bean.getClass().getMethods();
        for (Method m:method) {
            Broadcaster broadcaster=m.getAnnotation(Broadcaster.class);
            if(broadcaster!=null){
                if(broadcaster.className().equals("1") && broadcaster.methodName().equals("1")){
                    mappingMap.put(bean.getClass().getName()+m.getName(),bean.getClass().getName()+","+m.getName());
                }else {
                    mappingMap.put(broadcaster.className()+broadcaster.methodName(),bean.getClass().getName()+","+m.getName());
                }
            }
        }
        return bean;
    }


    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}
